package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLessonItem;
import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonRepository;
import com.china08.yxyapi.repository.db.YxyResPhaseRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;
import com.china08.yxyapi.service.yxyresource.impl.YxyResourceServiceImpl;

/**
 * 同步课程--详情页--上边和左边
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncParticularSrhController extends YxyApiBaseController {

    /** 目录 */
    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    /** 课程 */
    @Autowired
    private YxyLessonsSynchroLessonRepository yxyLessonsSynchroLessonRepository;

    /** 章节 */
    @Autowired
    private YxyLessonsSynchroLessonChapterRepository yxyLessonsSynchroLessonChapterRepository;

    /** 课时 */
    @Autowired
    private YxyLessonsSynchroLessonItemRepository yxyLessonsSynchroLessonItemRepository;

    /** 学段 */
    @Autowired
    private YxyResPhaseRepository yxyResPhaseRepository;

    /** 学科 */
    @Autowired
    private YxyResSubjectRepository yxyResSubjectRepository;

    /** 版本 */
    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    @Autowired
    private YxyResourceServiceImpl yxyResourceServiceImpl;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/particular/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        YxyLessonsSynchroLesson yxyLessonsSynchroLessons = yxyLessonsSynchroLessonRepository.findById(lessonId);
        RespModel model = new RespModel();
        model.cover = yxyLessonsSynchroLessons.getCover();
        model.name = yxyLessonsSynchroLessons.getName();
        model.count = yxyLessonsSynchroLessons.getCount();
        model.duration = yxyLessonsSynchroLessons.getDuration();
        model.ep = yxyLessonsSynchroLessons.getEp();
        model.vv = yxyLessonsSynchroLessons.getVv();
        model.summary = yxyLessonsSynchroLessons.getSummary();
        model.author = yxyLessonsSynchroLessons.getAuthor();
        model.avator = yxyLessonsSynchroLessons.getAvator();
        model.vip = yxyLessonsSynchroLessons.getVip();

        // 学段、学科、版本、分类
        YxyLessonsSynchroCatalog yxyLessonsSynchroCatalogs = yxyLessonsSynchroCatalogRepository
                .findById(yxyLessonsSynchroLessons.getCatalogId());
        YxyResPhase yxyResPhases = yxyResPhaseRepository.findById(yxyLessonsSynchroCatalogs.getStageId());
        YxyResSubject yxyResSubjects = yxyResSubjectRepository.findById(yxyLessonsSynchroCatalogs.getSubjectId());
        YxyResVersion yxyResVersions = yxyResVersionRepository.findById(yxyLessonsSynchroCatalogs.getVersionId());
        YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository
                .findById(yxyLessonsSynchroCatalogs.getGradeId());

        model.stageName = yxyResPhases.getName();
        model.subjectName = yxyResSubjects.getName();
        model.versionName = yxyResVersions.getName();
        model.gradeName = yxyLessonsSynchroGrades.getName();
        List<RespModel> returnLst = new ArrayList<>();

        // 章节、课时
        List<YxyLessonsSynchroLessonChapter> chapterLst = yxyLessonsSynchroLessonChapterRepository
                .findByLessonIdOrderByOrderNumAsc(lessonId);
        List<Chapter> chapterList = new ArrayList<Chapter>();
        for (YxyLessonsSynchroLessonChapter yxyLessonsSynchroLessonChapter : chapterLst) {
            Chapter chapter = new Chapter();
            chapter.ChapterIntro = yxyLessonsSynchroLessonChapter.getIntro();
            chapter.ChapterTitle = yxyLessonsSynchroLessonChapter.getTitle();
            chapterList.add(chapter);

            List<LessonItem> lessonItemList = new ArrayList<LessonItem>();
            List<YxyLessonsSynchroLessonItem> lessonItemLst = yxyLessonsSynchroLessonItemRepository
                    .findByChapterIdAndStatusNotOrderByOrderNumAsc(yxyLessonsSynchroLessonChapter.getId(), -1);
            for (YxyLessonsSynchroLessonItem yxyLessonsSynchroLessonItem : lessonItemLst) {
                LessonItem lessonItem = new LessonItem();
                // lessonItemId和userId
                // List<YxyLessonsSynchroLessonItem>
                // yxyLessonsSynchroLessonItemLst =
                // yxyLessonsSynchroLessonItemRepository
                // .findByLessonId(lessonId);
                // for (YxyLessonsSynchroLessonItem yxyLessonsSynchroLessonItem1
                // : yxyLessonsSynchroLessonItemLst) {
                lessonItem.lessonItem = yxyLessonsSynchroLessonItem.getId();
                ElasticYxyResResource videos = yxyResourceServiceImpl
                        .findById(yxyLessonsSynchroLessonItem.getWeikeId());
                if (videos != null) {
                    lessonItem.tvDuration = videos.getDuration();
                }
                // }
                lessonItem.lessonItemIntro = yxyLessonsSynchroLessonItem.getIntro();
                lessonItem.lessonItemTitle = yxyLessonsSynchroLessonItem.getTitle();
                lessonItem.preview = yxyLessonsSynchroLessonItem.getPreview();
                lessonItem.lessonIdItem = yxyLessonsSynchroLessonItem.getId();
                lessonItem.OrderNum = yxyLessonsSynchroLessonItem.getOrderNum() - 19;
                lessonItemList.add(lessonItem);
            }
            chapter.lessonItemList = lessonItemList;
            // chapterList.add(chapter);
        }
        model.chapterList = chapterList;
        returnLst.add(model);
        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 封面
        public String cover;
        // 课程名称
        public String name;

        // ###
        // 学段
        public String stageName;
        // 学年
        public String gradeName;
        // 学科
        public String subjectName;
        // 版本
        public String versionName;

        // ###
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程评分（所有评分之和处于评分人数即综合评分）
        public Float ep;
        // 学习人数
        public Integer vv;
        // 课程简介
        public String summary;
        // 贡献者
        public String author;
        // 贡献者头像
        public String avator;

        public Integer vip;

        // ###
        public List<Chapter> chapterList;

    }

    /** 章节 */
    public static class Chapter {

        // 章节名称
        public String ChapterTitle;
        // 章节简介
        public String ChapterIntro;

        public List<LessonItem> lessonItemList;
    }

    /** 课时 */
    public static class LessonItem {

        // 课时名称
        public String lessonItemTitle;
        // 课时简介
        public String lessonItemIntro;
        // 是否可以免费试看 0--不可以试看, 1--可以试看
        public Integer preview;
        // 课程ID
        public String lessonIdItem;
        // 课时Id
        public String lessonItem;
        // 视频时常
        public Integer tvDuration;
        // 序号
        public Integer OrderNum;

    }
}
